require(ggplot2)
library(haven)
library(grid)
library(gridExtra)
library(qpdf)

setwd("/Users/zimaoxiao/Dropbox/CC_Yield_Predict/replication_package/")
estimates = read_dta("data/figure/bin_estimates.dta")

estimates$spec = factor(estimates$label, c("State linear trends","State quadratic trends","State-by-year FE"))
layer_1 = subset(estimates, layer == 1)
layer_2 = subset(estimates, layer == 2)

base_font_size <- 12

A1 <- ggplot(layer_1, aes(x = xcoord, y = estimate)) +
  geom_point(size = 1.5) +
  geom_line(size = 0.5, alpha = 0.5) +
  geom_hline(yintercept = 0, linetype = 2, size = 0.5, color = "blue") +
  geom_ribbon(aes(ymin = estimate - 1.64 * stderr, ymax = estimate + 1.64 * stderr), alpha = 0.3) +
  geom_ribbon(aes(ymin = min95, ymax = max95), alpha = 0.3) +
  facet_wrap(~ label, ncol = 3) +
  xlab("Temperature Bins") + ylab("ln(Yields)") +
  scale_x_continuous(limits = c(0, 39), breaks = seq(0, 39, by = 3)) +
  ylim(-0.12, 0.05) +
  theme_bw() + 
  theme(strip.background = element_rect(fill = "white"),
        text = element_text(size = base_font_size),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size = base_font_size + 4, hjust = 0.5),
        axis.title.x = element_text(size = base_font_size + 2),
        axis.title.y = element_text(size = base_font_size + 2),
        axis.text = element_text(size = base_font_size),
        strip.text = element_text(size = base_font_size + 2),
        plot.margin = margin(t = 20, r = 20, b = 20, l = 20))

A2 <- ggplot(layer_2, aes(x = xcoord, y = estimate)) +
  geom_point(size = 1.5) +
  geom_line(size = 0.5, alpha = 0.5) +
  geom_hline(yintercept = 0, linetype = 2, size = 0.5, color = "blue") +
  geom_ribbon(aes(ymin = estimate - 1.64 * stderr, ymax = estimate + 1.64 * stderr), alpha = 0.3) +
  geom_ribbon(aes(ymin = min95, ymax = max95), alpha = 0.3) +
  facet_wrap(~ label, ncol = 3) +
  xlab("Temperature Bins") + ylab("ln(Yields)") +
  scale_x_continuous(limits = c(0, 39), breaks = seq(0, 39, by = 3)) +
  ylim(-0.12, 0.05) +
  theme_bw() + 
  theme(strip.background = element_rect(fill = "white"),
        text = element_text(size = base_font_size),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size = base_font_size + 4, hjust = 0.5),
        axis.title.x = element_text(size = base_font_size + 2),
        axis.title.y = element_text(size = base_font_size + 2),
        axis.text = element_text(size = base_font_size),
        strip.text = element_text(size = base_font_size + 2),
        plot.margin = margin(t = 20, r = 20, b = 20, l = 20))

A1 <- A1 + labs(title = "(A) Temperature-Yield Relationship: 3°C Bin, 1950-2000")
A2 <- A2 + labs(title = "(B) Temperature-Yield Relationship: 3°C Bin, 1950-2020")

grid_arranged <- grid.arrange(A1, A2, ncol = 1, nrow = 2)
png_width <- 12
png_height <- 9
png_dpi <- 300

ggsave("output/FigureS5.png",
       plot = grid_arranged, width = png_width, height = png_height, dpi = png_dpi)